<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="box">
      <table class="fruit">
        <thead>
          <tr>
            <th>选中</th>
            <th>单价</th>
            <th>个数</th>
            <th>小计</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in fruitList" :key="item.id">
            <td>
              <input type="checkbox" name="fruit" v-model="item.check" />
            </td>
            <td>{{ item.price }}</td>
            <td>
              <div class="count">
                <button @click="subCount(item.id)">-</button>
                <input type="text" v-model.number="item.count" />
                <button @click="addCount(item.id)">+</button>
              </div>
            </td>
            <td>{{ item.price * item.count }}</td>
            <td><button @click="del(item.id)">删除</button></td>
          </tr>
        </tbody>
        <tfoot>
          <input
            type="checkbox"
            @change="fruitList.forEach(item => item.check = allSelect)"
            name="fruit"
            v-model="allSelect"
          />
          <span>总价: {{ totalPrice }}</span>
          <button>结算({{ total }})</button>
        </tfoot>
      </table>
    </div>
    <script src="./vue.js"></script>
    <script>
      new Vue({
        el: "#box",
        data: {
          fruitList: [
            { id: 0, check: false, price: 3, count: 2 },
            { id: 1, check: false, price: 13, count: 1 },
            { id: 2, check: false, price: 4, count: 5 },
            { id: 3, check: false, price: 6, count: 3 },
            { id: 4, check: false, price: 1, count: 4 },
          ],
          allSelect: false,
        },
        computed: {
          subtotal() {
            return this.fruitList;
          },
          //   allSelect: {
          //     get() {
          //       return this.fruitList.every((item) => {
          //         return item.check;
          //       });
          //     },
          //     set(val) {
          //       this.fruitList.forEach((item) => {
          //         item.check = val;
          //       });
          //     },
          //   },
          total() {
            return this.fruitList
              .filter((item) => {
                return item.check;
              })
              .reduce((sum, item) => {
                return sum + item.count;
              }, 0);
          },
          totalPrice() {
            return this.fruitList
              .filter((item) => {
                return item.check;
              })
              .reduce((sum, item) => {
                return sum + item.count * item.price;
              }, 0);
          },
        },
        methods: {
          del(id) {
            this.fruitList = this.fruitList.filter((item) => {
              return item.id !== id;
            });
          },
          addCount(id) {
            let c_index = 0;
            this.fruitList.forEach((item, index) => {
              if (item.id === id) {
                c_index = index;
              }
            });
            if (this.fruitList[c_index].count >= 999) {
              this.fruitList[c_index].count = 999;
              return;
            }
            this.fruitList[c_index].count++;
          },
          subCount(id) {
            let c_index = 0;
            this.fruitList.forEach((item, index) => {
              if (item.id === id) {
                c_index = index;
              }
            });
            if (this.fruitList[c_index].count <= 0) {
              this.fruitList[c_index].count = 0;
              return;
            }
            this.fruitList[c_index].count--;
          },
        },
        watch: {
          fruitList: {
            immediate: true,
            deep: true,
            handler() {
              console.log(2);
              this.allSelect = this.fruitList.every((item) => item.check);
            },
          },
        },
      });
    </script>
  </body>
</html>
